package com.infore.airsupervise.adapter;

import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.utils.FileUtil;
import com.infore.airsupervise.utils.ToastUtil;
import com.vincent.filepicker.Util;

import java.util.List;

/**
 * 文件列表 展示 适配器
 * Created by xiaotao on 2017/5/18.
 */

public class FileListAdapter extends BaseQuickAdapter<String,BaseViewHolder>{
    private boolean deleteable ;


    public FileListAdapter(int layoutResId, List<String> data , boolean deleteable) {
        super(layoutResId, data);
        this.deleteable = deleteable;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final String item) {
        helper.setText(R.id.tv_file_name,item.substring(item.lastIndexOf("/")+1)); //文件名
        //根据后缀名判断文件
        final String fileSuffix = item.substring(item.lastIndexOf(".") + 1);
        helper.convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentOpenFile(item, FileUtil.getFileMIMEType(fileSuffix));
            }
        });
        if(deleteable) {
            helper.setVisible(R.id.iv_delete,true);
            //删除
            helper.getView(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    remove(helper.getAdapterPosition());
                }
            });
        }
    }

    private void intentOpenFile(String  filePath ,String dataType){
        Uri uri = Uri.parse("file://" +filePath);
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.setDataAndType(uri,dataType);
        if(Util.detectIntent(mContext,it)){
            mContext.startActivity(it);
        }else {
            ToastUtil.showShort(mContext,"对不起！没有找到可以打开这类文件的应用。");
        }
    }
}
